<?php

namespace App\Admin\Extensions;

use Encore\Admin\Admin;

class Remission
{
    protected $id;

    public function __construct($oid, $product_type)
    {
        $this->oid = $oid;
        $this->product_type = $product_type;
    }

    protected function script()
    {
        return <<<SCRIPT

layui.use('layer', function(){
        var layer = layui.layer;
        $('.remission').click(function () {
           var that = this;
           var oid = $(that).attr('data-oid');
           var product_type = $(that).attr('data-type');
           if(product_type == 2){
               var index = layer.open({
                    title: '减免',
                    type: 2,
                    content: '/admin/common/order/divide/remission?oid='+oid,
                    area: ['60%','80%'],
                    btn: ['确定','取消'],
                    yes: function(index, layero){
                        var layer_obj = $(layero).find("iframe").contents()
                        var remission = layer_obj.find("input[name=remission]");
                        var is_black = layer_obj.find("input[type=radio]:checked").val();
                        
                        var order_arr = {};
                        remission.each(function(index, element){
                            order_arr[$(this).attr('data-id')] = $(this).val();
                        })
                        
                        var remission_arr =  JSON.stringify(order_arr)
                        $.ajax({
                            url: '/admin/common/order/divide/remission',
                            type: 'post',
                            data: {
                                oid: oid,
                                remission: remission_arr,
                                black: is_black
                            },
                            success:function(res){
                                if(res.code == 0){
                                    layer.msg(res.text);
                                    layer.close(index);
                                    $(".container-refresh").click();
                                }else{
                                    layer.msg(res.text);
                                }
                            }
                        })
                    },
                    btn2: function(index, layero){
                        layer.close(index);
                    }
                });
                return;
           }
           var index = layer.open({
                title: '减免',
                type: 2,
                content: '/admin/common/remission?oid='+oid,
                area: ['350px','265px'],
                btn: ['确定','取消'],
                yes: function(index, layero){
                    var mission_mon = $(layero).find("iframe").contents().find(".remiss").val();
                    var should_paymon = $(layero).find("iframe").contents().find(".should_pay").text();
                    var is_black = $(layero).find("iframe").contents().find("input[type=radio]:checked").val();
                    
                    if(mission_mon == ''){
                        layer.msg('金额不能小于0');
                        return;
                    }
                   
                    if(mission_mon * 1 > should_paymon * 1){
                        layer.msg('免减金额不能大于应还金额');
                        return;
                    }
                    
                    $.ajax({
                        url: '/admin/common/remission',
                        type: 'post',
                        data: {
                            oid: oid,
                            money: mission_mon,
                            black: is_black
                        },
                        success:function(res){
                            if(res.code == 0){
                                layer.msg(res.text);
                                layer.close(index);
                                location.href = location.href;
                            }else{
                                layer.msg(res.text);
                            }
                        }
                    })
                },
                btn2: function(index, layero){
                    layer.close(index);
                }
            });
       });
});

SCRIPT;
    }

    protected function render()
    {
        Admin::script($this->script());

        return "&nbsp;<a href='javascript:void(0);'><button data-type='{$this->product_type}'  data-oid='{$this->oid}' class='remission layui-btn  layui-btn-normal layui-btn-xs'>减</button></a>";

    }

    public function __toString()
    {
        return $this->render();
    }
}